﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcLibrary.Repository;
using System.IO;
using WebVideo.Areas.Admin.Models;

namespace WebVideo.Areas.Admin.Controllers
{
    public class Image_VideoController : Controller
    {
        //
        // GET: /Admin/Image_Video/

        public ActionResult Index(int Id)
        {
            ViewBag.IdVideo = Id;
            return View(Image_VideoModel.LayDuLieu (Id));
        }


        #region "Insert"
        public ActionResult Insert(int IdVideo)
        {
            Image_VideoModel tnew = new Image_VideoModel();
            tnew.IdVideo = IdVideo;
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityQuestions_MultipleChoices
                        where p.IdVideo == IdVideo 
                        select p;
            tnew.DSQuestion = query.ToList();
            return View(tnew);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(Image_VideoModel Mtnew, HttpPostedFileBase Image)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Hình ảnh phải bé hơn 2MB." });
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Không thể Upload hình" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }
                }

                Image_VideoModel.Insert(Mtnew);
                return RedirectToAction("Index", "Image_Video", new { Id = Mtnew.IdVideo });

            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Không có câu hỏi để chọn hình ảnh, bạn vui lòng thêm câu hỏi trắc nghiệm cho video này" });
            }

            //return View(Mtnew);
        }
        #endregion //insert


        #region "Edit"
        public ActionResult Edit(int IdQVideo)
        {
            return View(Image_VideoModel.LayModel(IdQVideo));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Image_VideoModel Mtnew, HttpPostedFileBase Image)
        {

            try
            {
                if (ModelState.IsValid)
                {
                        if (Image != null && Image.ContentLength > 0)
                        {
                            int kb = Image.ContentLength / 1024; //file size kb
                            if (kb >= 2048) // file qua lon
                            {
                                return RedirectToAction("Index", "Error", new { errorMsg = "Hình ảnh phải bé hơn 2MB." });
                            }

                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(Image);

                            if (encodestring == "!")
                                return RedirectToAction("Index", "Error", new { errorMsg = "Không thể Upload hình" });

                            var path = Path.Combine(Server.MapPath("~/Content/Images"), encodestring);
                            Image.SaveAs(path);

                            Mtnew.Image = encodestring;
                        }


                   string HinhAnh = Image_VideoModel.Edit(Mtnew);
                   if (HinhAnh != null)
                   {
                       string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images"), HinhAnh); // file hinh cu
                       System.IO.File.Delete(fileToDelete);
                   }
                    return RedirectToAction("Index", "Image_Video", new { Id = Mtnew.IdVideo });
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //Edit



        #region "Delete"

        public string Delete(int Id)
        {
            try
            {
                string HinhAnh = Image_VideoModel.Delete(Id);
                if (HinhAnh != null)
                {
                    string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images"), HinhAnh); // file hinh cu
                    System.IO.File.Delete(fileToDelete);
                }

                return "ok";

            }
            catch
            {
                return "error";

            }

        }
        #endregion //Delete
    }
}
